package com.firsteapps.mobile.android.puzzle;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.firsteapps.mobile.android.puzzle.game.Helper;
import com.firsteapps.mobile.android.puzzle.game.PuzzleInfo;
import com.firsteapps.mobile.android.puzzle.util.Consts;

public class RunOut extends PostLoginBase {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.run_out);

		int runoutType = getIntent().getIntExtra(Consts.INFO_RUNOUT_TYPE, Consts.RUNOUT_TIME);
		final String infoFileName = getIntent().getStringExtra(Consts.PUZZLE_FILE_NAME);
		final PuzzleInfo info = Helper.loadCreatedPuzzle(this, infoFileName);
		// (PuzzleInfo) getIntent().getSerializableExtra(Consts.PUZZLE_KEY);

		TextView puzzleSizeViewer = (TextView) findViewById(R.id.puzzle_size_viewer);
		TextView movesCountViewer = (TextView) findViewById(R.id.moves_count_viewer);
		TextView attemptsCountViewer = (TextView) findViewById(R.id.attempts_count_viewer);
		TextView timeLimitViewer = (TextView) findViewById(R.id.time_limit_viewer);
		TextView autosolveViewer = (TextView) findViewById(R.id.autosolve_viewer);

		puzzleSizeViewer.setText(info.getSize() + ":" + info.getSize());
		movesCountViewer.setText(info.getMoveCount() + (info.getMaxMoves() > 0 ? "/" + info.getMaxMoves() : ""));
		attemptsCountViewer.setText(info.getAttempts() + (info.getMaxAttempts() > 0 ? "/" + info.getMaxAttempts() : ""));
		timeLimitViewer.setText(Helper.getElapsedTime(info.getElapsed())
				+ (info.getMaxTime() > 0 ? "/" + Helper.getElapsedTime(info.getMaxTime() * 1000) : ""));
		autosolveViewer.setText(info.isAutosolvable() ? "YES" : "NO");

		TextView runoutInfo = (TextView) findViewById(R.id.info_runout_type);
		TextView hiddenText = (TextView) findViewById(R.id.runout_hidden_text);
		ImageView hiddenImage = (ImageView) findViewById(R.id.runout_hidden_image);
		ImageView runoutAutosolve = (ImageView) findViewById(R.id.autosolve_puzzle);
		runoutAutosolve.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Helper.autosolvePuzzle(RunOut.this, infoFileName, info);
			}
		});

		System.out.println(infoFileName);

		switch (runoutType) {
		case Consts.RUNOUT_ATTEMPTS:
			runoutInfo.setText(R.string.runout_attempts);
			break;
		case Consts.RUNOUT_MOVES:
			runoutInfo.setText(R.string.runout_moves);
			info.resetPuzzleAfterRunout();
			Helper.saveOngoingPuzzle(RunOut.this, infoFileName, info);
			break;
		case Consts.RUNOUT_TIME:
			runoutInfo.setText(R.string.runout_time);
			info.resetPuzzleAfterRunout();
			Helper.saveOngoingPuzzle(RunOut.this, infoFileName, info);
			break;
		case Consts.RUNOUT_NONE:
			if (!info.isCompleted()) {
				info.setCompleted(true);
				Helper.saveOngoingPuzzle(RunOut.this, infoFileName, info);

				int moves = info.getMoveCount();
				int time = (int) (info.getElapsed() / 1000);
				HighScore highScore = new HighScore(Helper.getUserName(getUserName()), moves, time, Helper.getScore(moves, time,
						info.getSize()));

				String[] oldScores = Helper.getPuzzleData(RunOut.this, Consts.FILE_NAME_HIGHSCORES);
				String[] newScores;
				if (oldScores != null && oldScores.length > 0) {
					newScores = new String[oldScores.length + 1];
					for (int i = 0; i < oldScores.length; ++i) {
						newScores[i] = oldScores[i];
					}
					newScores[oldScores.length] = highScore.toString();
				} else {
					newScores = new String[] { highScore.toString() };
				}

				Helper.savePuzzleData(RunOut.this, Consts.FILE_NAME_HIGHSCORES, newScores);
			}

			findViewById(R.id.runout_select).setVisibility(View.GONE);
			runoutAutosolve.setVisibility(View.GONE);
			hiddenText.setVisibility(View.VISIBLE);
			hiddenText.setText(info.getAlternativeText() == null ? "NO MESSAGES" : info.getAlternativeText());
			hiddenImage.setVisibility(View.VISIBLE);
			String imagefile = info.getAlternativeImage() == null ? info.getFile() : info.getAlternativeImage();

			// Uri uri = Uri.fromFile(new File(RunOut.this.getFilesDir() + "/" + imagefile));

			if (hiddenImage.getDrawable() != null)
				((BitmapDrawable) hiddenImage.getDrawable()).getBitmap().recycle();

			hiddenImage.setImageBitmap(null);
			hiddenImage.setImageBitmap(Helper.decodeBitmapFromPath(RunOut.this.getFilesDir() + "/" + imagefile, 886, 490));

			// hiddenImage.setImageURI(uri);

			Thread registerCompletedPuzzle = new Thread(new Runnable() {
				@Override
				public void run() {
					if (Helper.unRegisterPuzzle(RunOut.this, getWorkingDirectory() + Consts.FOLDER_NAME_INCOMPLETE_PUZZLES, infoFileName))
						Helper.registerPuzzle(RunOut.this, getWorkingDirectory() + Consts.FOLDER_NAME_COMPLETED_PUZZLES, infoFileName);
				}
			});
			registerCompletedPuzzle.start();
			break;
		default:
			break;
		}
	}
}
